import os
import os.path
import xml.dom.minidom
import xml.etree.cElementTree as et
import matplotlib.pyplot as plt

 
 
path="all_xml_104806"
files=os.listdir(path)
s=[]

img_4k_square_list_1x = [str('>16*16'), str('16*16 - 32*32'), str('32*32 - 96*96'), str('96*96<')]
img_4k_square_list_2x = [str('>32*32'), str('32*32 - 64*64'), str('64*64 - 192*192'), str('192*192<')]
img_2k_square_list = [str('>16*16'), str('16*16 - 32*32'), str('32*32 - 96*96'), str('96*96<')]

img_4k_square_list_16_1x = []
img_4k_square_list_32_1x = []
img_4k_square_list_96_1x = []
img_4k_square_list_96_1x_ = []
img_4k_square_list_32_2x = []
img_4k_square_list_64_2x = []
img_4k_square_list_192_2x = []
img_4k_square_list_192_2x_ = []
img_4k_object_list = []

img_2k_square_list_16 = []
img_2k_square_list_32 = []
img_2k_square_list_96 = []
img_2k_square_list_96_ = []
img_2k_object_list = []

side_list = []
n = 0
m = 0

# =============================================================================
# extensional filename
# =============================================================================
def file_extension(path): 
    return os.path.splitext(path)[1] 
 
for xmlFile in files: 
    if not os.path.isdir(xmlFile): 
        if file_extension(xmlFile) == '.xml':
            # print(os.path.join(path,xmlFile))
            tree=et.parse(os.path.join(path,xmlFile))
            root=tree.getroot()
            filename=root.find('filename').text
        #    print("filename is", path + '/' + xmlFile)
            DOMTree = xml.dom.minidom.parse(os.path.join(path, xmlFile))
            collection = DOMTree.documentElement
            W = float(collection.getElementsByTagName("width")[0].childNodes[0].data)
            H = float(collection.getElementsByTagName("height")[0].childNodes[0].data)
            if W == 3840 and H == 2160:
                n = n + 1
                for Object in root.findall('object'):
                #    name=Object.find('name').text
                #    print("Object name is ", name)
                    img_4k_object_list.append(Object)
                    bndbox=Object.find('bndbox')
                    xmin=bndbox.find('xmin').text
                    ymin=bndbox.find('ymin').text
                    xmax=bndbox.find('xmax').text
                    ymax=bndbox.find('ymax').text
                    square = (int(ymax)-int(ymin)) * (int(xmax)-int(xmin))
                    if square <= 16*16:
                        img_4k_square_list_16_1x.append(square)
                    if 16*16 < square <= 32*32:
                        img_4k_square_list_32_1x.append(square)
                    if 32*32 < square <= 96*96:
                        img_4k_square_list_96_1x.append(square)
                    if square > 96*96:
                        img_4k_square_list_96_1x_.append(square)

                    if square <= 32*32:
                        img_4k_square_list_32_2x.append(square)
                    if 32*32 < square <= 64*64:
                        img_4k_square_list_64_2x.append(square)
                    if 64*64 < square <= 192*192:
                        img_4k_square_list_192_2x.append(square)
                    if square > 192*192:
                        img_4k_square_list_192_2x_.append(square)

                    
                # for Object in root.findall('size'):
                #     side1=Object.find('width').text
                #     side2=Object.find('height').text
                #     side_list.append(int(side1))
                #     side_list.append(int(side2))
                #     print(xmin,ymin,xmax,ymax)
                #     print(square)

            if W == 1920 and H == 1080:
                m = m + 1
                for Object in root.findall('object'):
                #    name=Object.find('name').text
                #    print("Object name is ", name)
                    img_2k_object_list.append(Object)
                    bndbox=Object.find('bndbox')
                    xmin=bndbox.find('xmin').text
                    ymin=bndbox.find('ymin').text
                    xmax=bndbox.find('xmax').text
                    ymax=bndbox.find('ymax').text
                    square = (int(ymax)-int(ymin)) * (int(xmax)-int(xmin))
                    if square <= 16*16:
                        img_2k_square_list_16.append(square)
                    if 16*16 < square <= 32*32:
                        img_2k_square_list_32.append(square)
                    if 32*32 < square <= 96*96:
                        img_2k_square_list_96.append(square)
                    if square > 96*96:
                        img_2k_square_list_96_.append(square)

                    
                # for Object in root.findall('size'):
                #     side1=Object.find('width').text
                #     side2=Object.find('height').text
                #     side_list.append(int(side1))
                #     side_list.append(int(side2))
                #     print(xmin,ymin,xmax,ymax)
                #     print(square)
                
img_4k_square_list_num_1x = [len(img_4k_square_list_16_1x), len(img_4k_square_list_32_1x), len(img_4k_square_list_96_1x), len(img_4k_square_list_96_1x_)]
img_4k_percent_list_1x = [float(round(len(img_4k_square_list_16_1x)/len(img_4k_object_list)*100, 2)), float(round(len(img_4k_square_list_32_1x)/len(img_4k_object_list)*100, 2)), 
                float(round(len(img_4k_square_list_96_1x)/len(img_4k_object_list)*100, 2)), float(round(len(img_4k_square_list_96_1x_)/len(img_4k_object_list)*100, 2))]

img_4k_square_list_num_2x = [len(img_4k_square_list_32_2x), len(img_4k_square_list_64_2x), len(img_4k_square_list_192_2x), len(img_4k_square_list_192_2x_)]
img_4k_percent_list_2x = [float(round(len(img_4k_square_list_32_2x)/len(img_4k_object_list)*100, 2)), float(round(len(img_4k_square_list_64_2x)/len(img_4k_object_list)*100, 2)), 
                float(round(len(img_4k_square_list_192_2x)/len(img_4k_object_list)*100, 2)), float(round(len(img_4k_square_list_192_2x_)/len(img_4k_object_list)*100, 2))]

img_2k_square_list_num = [len(img_2k_square_list_16), len(img_2k_square_list_32), len(img_2k_square_list_96), len(img_2k_square_list_96_)]
img_2k_percent_list = [float(round(len(img_2k_square_list_16)/len(img_2k_object_list)*100, 2)), float(round(len(img_2k_square_list_32)/len(img_2k_object_list)*100, 2)), 
                float(round(len(img_2k_square_list_96)/len(img_2k_object_list)*100, 2)), float(round(len(img_2k_square_list_96_)/len(img_2k_object_list)*100, 2))]


#刻度距离坐标轴的距离调整
plt.tick_params(pad = 0.03)  #通过pad参数调整距离

#data_collection_location
plt.bar(img_4k_square_list_1x, img_4k_square_list_num_1x, color = 'g', align =  'center')
plt.title('Bosch 2D Detection')
plt.text(len(img_4k_square_list_1x)/2-0.5, max(img_4k_square_list_num_1x)*1.15, list(img_4k_square_list_num_1x), ha='center', fontsize=12)
plt.text(len(img_4k_square_list_1x)/2-0.5, max(img_4k_square_list_num_1x)*1.25, "percent is %s"% list(img_4k_percent_list_1x), ha='center', fontsize=12)
plt.text(len(img_4k_square_list_1x)/2-0.5, max(img_4k_square_list_num_1x)*1.35, "bbox of dataset is %s"% len(img_4k_object_list), ha='center', fontsize=12)
plt.text(len(img_4k_square_list_1x)/2-0.5, max(img_4k_square_list_num_1x)*1.45, "num of 4k-image is %s"% n, ha='center', fontsize=12)
plt.ylabel('Location axis',fontsize=11) 
plt.xlabel('Num axis',fontsize=11,linespacing = 0.3, labelpad = 0.3) 
plt.savefig('img_4k_square_list_num_1x.png', bbox_inches='tight')
plt.close()

#data_collection_location
plt.bar(img_4k_square_list_2x, img_4k_square_list_num_2x, color = 'g', align =  'center')
plt.title('Bosch 2D Detection')
plt.text(len(img_4k_square_list_2x)/2-0.5, max(img_4k_square_list_num_2x)*1.15, list(img_4k_square_list_num_2x), ha='center', fontsize=12)
plt.text(len(img_4k_square_list_2x)/2-0.5, max(img_4k_square_list_num_2x)*1.25, "percent is %s"% list(img_4k_percent_list_2x), ha='center', fontsize=12)
plt.text(len(img_4k_square_list_2x)/2-0.5, max(img_4k_square_list_num_2x)*1.35, "bbox of dataset is %s"% len(img_4k_object_list), ha='center', fontsize=12)
plt.text(len(img_4k_square_list_2x)/2-0.5, max(img_4k_square_list_num_2x)*1.45, "num of 4k-image is %s"% n, ha='center', fontsize=12)
plt.ylabel('Location axis',fontsize=11) 
plt.xlabel('Num axis',fontsize=11,linespacing = 0.3, labelpad = 0.3) 
plt.savefig('img_4k_square_list_num_2x.png', bbox_inches='tight')
plt.close()


#data_collection_location
plt.bar(img_2k_square_list, img_2k_square_list_num, color = 'g', align =  'center')
plt.title('Bosch 2D Detection')
plt.text(len(img_2k_square_list)/2-0.5, max(img_2k_square_list_num)*1.15, list(img_2k_square_list_num), ha='center', fontsize=12)
plt.text(len(img_2k_square_list)/2-0.5, max(img_2k_square_list_num)*1.25, "percent is %s"% list(img_2k_percent_list), ha='center', fontsize=12)
plt.text(len(img_2k_square_list)/2-0.5, max(img_2k_square_list_num)*1.35, "bbox of dataset is %s"% len(img_2k_object_list), ha='center', fontsize=12)
plt.text(len(img_2k_square_list)/2-0.5, max(img_2k_square_list_num)*1.45, "num of 2k-image is %s"% m, ha='center', fontsize=12)
plt.ylabel('Location axis',fontsize=11) 
plt.xlabel('Num axis',fontsize=11,linespacing = 0.3, labelpad = 0.3) 
plt.savefig('img_2k_square_list_num.png', bbox_inches='tight')
plt.close()